package tx;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

public class EmployeeServiceImpl {
  private DeptDaoImpl deptDao;
  private EmployeeDaoImpl employeeDao;
  private TransactionTemplate transactionTemplate;

  public TransactionTemplate getTransactionTemplate() {
    return transactionTemplate;
  }

  public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
    this.transactionTemplate = transactionTemplate;
  }

  public DeptDaoImpl getDeptDao() {
    return deptDao;
  }

  public void setDeptDao(DeptDaoImpl deptDao) {
    this.deptDao = deptDao;
  }

  public EmployeeDaoImpl getEmployeeDao() {
    return employeeDao;
  }

  public void setEmployeeDao(EmployeeDaoImpl employeeDao) {
    this.employeeDao = employeeDao;
  }

  public void insert() {
    transactionTemplate.execute(new TransactionCallback() {
      @Override
      public Object doInTransaction(TransactionStatus status) {
        employeeDao.delete();
        int rows = deptDao.insert();
        return rows;
      }
    });
  }

}
